package com.xinyu126.net_trace.share

import android.location.Location
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import com.xinyu126.net_trace.share.ui.theme.MagicalLocationTheme
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapView
import androidx.compose.ui.viewinterop.AndroidView
import com.baidu.location.LocationClient
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.magical.location.client.ILocationAble
import com.magical.location.client.ILocationClient
import com.magical.location.client.LocationListener
import com.xinyu126.net_trace.share.layers.ClientPointMarker
import com.xinyu126.net_trace.share.layers.ClientTrace
import com.xinyu126.net_trace.share.layers.IMarker

class MapActivity : ComponentActivity() {

    private lateinit var mapView: MapView
    private lateinit var baiduMap: BaiduMap
    private lateinit var locationClient: ILocationClient
    private lateinit var marker: IMarker
    private lateinit var listener: LocationListener

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        enableEdgeToEdge()
        mapView = MapView(this)

        setContent {
            MagicalLocationTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(innerPadding)
                    ) {
                        // 地图视图
                        BaiduMapView(mapView,
                            modifier = Modifier
                                .weight(1f)
                                .fillMaxWidth()
                        )
                        

                    }
                }
            }
        }

        baiduMap = mapView.map
        baiduMap.isMyLocationEnabled = true
        LocationClient .setAgreePrivacy(true)
        locationClient = (application as ILocationAble).client
        listener = MyLocationListener(baiduMap)
        locationClient.start()
        locationClient.addListener(listener)
        if(savedInstanceState != null) {
            val myId = savedInstanceState.getString("myId")?:"1"
            val clientId = savedInstanceState.getString("client_id")
            Log.d("MapActivity", "onCreate trace client: $clientId")
            marker = if(!clientId.isNullOrEmpty()) {
                ClientTrace(baiduMap,myId,clientId)
            }else{
                ClientPointMarker(baiduMap,myId)
            }
        }else{
            marker = ClientPointMarker(baiduMap,"")
        }
        marker.start(this)
    }

    override fun onDestroy() {
        baiduMap.isMyLocationEnabled = false
        marker.stop()
        locationClient.removeListener(listener)
        mapView.onDestroy()
        super.onDestroy()
    }

}

class MyLocationListener(private var baiduMap: BaiduMap) : LocationListener {
    private var firstLoc: LatLng? = null
    override fun onLocationChanged(location: Location) {
        val locData = MyLocationData.Builder()
            .accuracy(location.accuracy)
            .direction(location.bearing)
            .latitude(location.latitude)
            .longitude(location.longitude)
            .build()
        baiduMap.setMyLocationData(locData)
        if(firstLoc == null) {
            firstLoc = LatLng(location.latitude, location.longitude)
            baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(firstLoc))
        }
    }
}

@Composable
fun BaiduMapView(map:MapView,modifier: Modifier = Modifier) {
    AndroidView(
        modifier = modifier,
        factory = {
            map
        }
    )
}